//
//  View.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/8/31.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit
extension UIView {
    //给view添加阴影
    func addShadowInViewPrivite() {
        //阴影的颜色
        self.layer.shadowColor = ColorWithHex("#0C2A59").cgColor;
            //阴影的透明度
        self.layer.shadowOpacity = 0.3;
            //阴影发散的程度
        self.layer.shadowRadius = 4;
            //阴影偏移量
        self.layer.shadowOffset = CGSize(width: 0,height: 4);
        self.layer.masksToBounds = false;
    }
    //四周添加阴影
    func addShadowInViewBound() {
        //阴影的颜色
        self.layer.cornerRadius = 4;
        self.layer.shadowColor = ColorWithHex("#0C2A59").cgColor;
            //阴影的透明度
        self.layer.shadowOpacity = 0.08;
            //阴影发散的程度
        self.layer.shadowRadius = 5;
            //阴影偏移量
        self.layer.shadowOffset = CGSize(width: 0,height: 0);
        self.layer.masksToBounds = false;
    }
    // 单边阴影 顶边
    func addTopShadowInView() {
        //阴影的颜色
        self.layer.shadowColor = ColorWithHex("#0C2A59").cgColor;
            //阴影的透明度
        self.layer.shadowOpacity = 0.04;
            //阴影发散的程度
        self.layer.shadowRadius = 4;
            //阴影偏移量
        self.layer.shadowOffset = CGSize(width: 0,height: 0);
        self.layer.masksToBounds = false;
        // 单边阴影 顶边
        let shadowPathWidth = self.layer.shadowRadius
        let shadowRect = CGRect(x: 0, y: 0-shadowPathWidth/2.0, width: self.bounds.size.width, height: shadowPathWidth)
        let path = UIBezierPath(rect: shadowRect)
        self.layer.shadowPath = path.cgPath
    }
    //添加投影
    func addShadowInView(superView:UIView) {
        // 阴影 圆角和阴影无法在一个layer上共存，所以需要思路是再创建一个layer层，实现阴影，放到视图下边，
        let subLayer = CALayer();
        let fixframe = self.frame;
        subLayer.frame = fixframe;
        subLayer.cornerRadius = 6;
        subLayer.masksToBounds = false;
        subLayer.shadowColor = ColorWithHex("#0C2A59").cgColor;//shadowColor阴影颜色
        subLayer.shadowOffset = CGSize(width: 0,height: 0);//shadowOffset阴影偏移
        subLayer.shadowOpacity = 0.4;//阴影透明度，默认0
        subLayer.shadowRadius = 4;//阴影半径，默认3
        superView.layer.addSublayer(subLayer);
        subLayer.addSublayer(self.layer);
    }
    /// 无数据视图
    /// - Parameters:
    ///   - frame: 坐标
    ///   - text: 文字提示
    ///   - image: 图片展示
    func createNoMoreViewWithFrame(frame:CGRect,text:String,image:UIImage) {
        let view = UIView(frame: frame)
        view.backgroundColor = .clear
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 196, height: 121))
        imageView.image = image
        imageView.center = CGPoint(x: DeviceWidth/2, y: 80+(DeviceWidth*0.26+40)/2)
        if(frame.size.height<=(80+(DeviceWidth*0.26+40)/2)*2) {
            imageView.center = CGPoint(x:DeviceWidth/2, y:frame.size.height/2);
        }
        view.addSubview(imageView)
        let textLabel = UILabel.createLabel(frame: CGRect(x: leftDistance, y: imageView.maxY, width: DeviceWidth-2*leftDistance, height: 20), title: text, textColor: ColorWithHex("#9EAABD"), isCenter: true, font: 12, board: false)
        view.addSubview(textLabel)
        self.addSubview(view)
    }
    func createNoMoreViewWithFrame(frame:CGRect,text:String) {
        createNoMoreViewWithFrame(frame: frame, text: text, image: UIImage(named: "noHistory")!)
    }
    func createNoMoreViewWithFrame(frame:CGRect) {
        createNoMoreViewWithFrame(frame: frame, text: "暂无数据")
    }
    /***********************/
    var viewWidth: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }
    var viewHeight: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }
    var viewX: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }
    var viewY: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    var viewBottom: CGFloat {
        get {
            return viewY+viewHeight
        }
    }
    var viewRight: CGFloat {
        get {
            return viewX+viewWidth
        }
    }
    var minX: CGFloat {
        get {
            return self.frame.minX
        }
    }
    var midX: CGFloat {
        get {
            return self.frame.midX
        }
    }
    var maxX: CGFloat {
        get {
            return self.frame.maxX
        }
    }
    var minY: CGFloat {
        get {
            return self.frame.minY
        }
    }
    var midY: CGFloat {
        get {
            return self.frame.midY
        }
    }
    var maxY: CGFloat {
        get {
            return self.frame.maxY
        }
    }
    
}
